package com.jk.libsm.service.impl;

import com.jk.libsm.mapper.BookMapper;
import com.jk.libsm.service.Bookservice;
import com.jk.libsm.utils.DateUtil;
import com.jk.libsm.vo.Bookinfoone;
import com.jk.libsm.vo.Books;
import com.jk.libsm.vo.Booktop;
import com.jk.libsm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jk.libsm.pojo.Book;
import com.jk.libsm.pojo.Borrowandreturn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lk
 * @date 2020/7/6 0006 23:17
 * @description:
 */
@Service
public class BookServiceImpl implements Bookservice {
    @Autowired
    BookMapper bookMapper;

    @Override
    public Result bookFindbooks(String token, int type, String info) {
        ArrayList<Books> list;
        int flag = 1;//可接书状态（1）
        if (type == 0) {//书名
            list = bookMapper.getAllBooksByName(info, flag);
        } else if (type == 1) {//种类
            list = bookMapper.getAllBooksByKind(info, flag);
        } else if (type == 2) {//出版社
            list = bookMapper.getAllBooksByPress(info, flag);
        } else if (type == 3) {//作者
            list = bookMapper.getAllBooksByAuthor(info, flag);
        } else if (type == 4) {//位置
            list = bookMapper.getAllBooksByPosition(info, flag);
        } else {//有误
            list = null;
        }
        Result result;
        if (list == null) {
            result = new Result("400", "处理失败", "");
        } else {
            ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
            for (Books books : list) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("book_id",books.getBNo());
                map.put("book_name",books.getBName());
                map.put("book_author",books.getBauthor());
                map.put("book_type",books.getBKName());
                map.put("book_press",books.getBpublic());
                map.put("position",books.getPosition());
                list1.add(map);
            }
            result = new Result("200", "处理成功", list1);
        }
        return result;
    }

    @Override
    public Result bookFindTopBooks(String token) {
        ArrayList<Booktop> list;
        list = bookMapper.getBooktops();
        ArrayList<Map<String,Object>> list2=new ArrayList<>();
        for(Booktop booktop:list){
            HashMap<String,Object> map=new HashMap<>();
            map.put("book_id",booktop.getBNo());
            map.put("book_name",booktop.getBName());
            map.put("book_author",booktop.getBauthor());
            map.put("book_type",booktop.getBKName());
            map.put("book_press",booktop.getBpublic());
            map.put("borrow_times",booktop.getBorrowCount());
            list2.add(map);
        }
        Result result = new Result("200", "处理成功", list2);
        return result;
    }

    @Override
    public Result findBorrowBooks() {
        ArrayList<Booktop> list;
        list = bookMapper.getBookBorrow();
        ArrayList<Map<String, Object>> list1 = new ArrayList<>();
        for (Booktop booktop : list) {
            HashMap<String, Object> map = new HashMap<>();
            Bookinfoone bookinfoone = new Bookinfoone();
            bookinfoone.setBook_id(booktop.getBNo() + "");
            bookinfoone.setBook_author(booktop.getBauthor());
            bookinfoone.setBook_name(booktop.getBName());
            bookinfoone.setBook_type(booktop.getBKName());
            bookinfoone.setBook_press(booktop.getBpublic());
            map.put("bookinfo", bookinfoone);
            map.put("remain_date", DateUtil.releaseDay(booktop.getShouldreturndate(),DateUtil.getNowDate()));//计算剩余天数
            map.put("borrow_user_id", booktop.getRNo());
            list1.add(map);
        }
        Result result = new Result("200", "处理成功", list1);
        return result;
    }

    @Override
    public Result findOverBooks(String token) {
        ArrayList<Borrowandreturn> list = bookMapper.getBorrowandreturn(DateUtil.getNowDate());
        ArrayList<Map<String, Object>> list1 = new ArrayList<>();
        for (Borrowandreturn borrowandreturn : list) {
            HashMap<String, Object> map = new HashMap<>();
            Book book = bookMapper.getBookByBNo(borrowandreturn.getBNo());
            String book_type = bookMapper.getBKNmameByBNo(borrowandreturn.getBNo());
            Bookinfoone bookinfoone = new Bookinfoone(book.getBNo()+"",book.getBName(),book.getBauthor(),book_type,book.getBpublic());
            map.put("book_info",bookinfoone);
            map.put("over_date",DateUtil.releaseDay(DateUtil.getNowDate(),borrowandreturn.getShouldreturndate()));
            map.put("borrow_user_id",borrowandreturn.getRNo());
            list1.add(map);
        }
        Result result = new Result("200", "处理成功", list1);
        return result;

    }
}
